﻿/// <reference name="MicrosoftAjax.js"/>

Type.registerNamespace('Microsoft.InformationSecurity.CISF.Web.AjaxExtenders');

Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior = function(element) {
Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.initializeBase(this, [element]);
}

Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.prototype = {
    initialize: function() {
        Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.callBaseMethod(this, 'initialize');

        var dateTimeElement = this._element;
        if (!dateTimeElement) throw Error.invalidOperation('Element required');
        this._setLocalTime();
    },
    dispose: function() {
        //Add custom dispose actions here
        //$removeHandler(window.document.body, "onmouseover", this._setLocalTime);
        Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.callBaseMethod(this, 'dispose');
    }
    //custom function to do the date time transformation
, _setLocalTime: function() {
    var dateTimeElement = this.get_element();
    if (!dateTimeElement) return;
    var passedDate = new Date();
    if (dateTimeElement.tagName.toUpperCase() == 'SPAN') {
        passedDate = new Date(dateTimeElement.innerText);
    }
    else if (dateTimeElement.tagName.toUpperCase() == 'INPUT') {
        passedDate = new Date(dateTimeElement.value);
    }
    if (!isNaN(passedDate.getFullYear())) {
        var newDateFormatted = '';
        var newDate = new Date(Date.UTC(passedDate.getFullYear(), passedDate.getMonth(), passedDate.getDate(), passedDate.getHours(), passedDate.getMinutes(), passedDate.getSeconds(), passedDate.getMilliseconds()));
        switch (this.get_DateTimeFormatString().toLowerCase()) {
            case 'd':
                newDateFormatted = this._formatDateTime(newDate, 'd');
                break;
            case 't':
                newDateFormatted = this._formatDateTime(newDate, 't');
                break;
            case 'g':
                newDateFormatted = this._formatDateTime(newDate, 'd') + ' ' + this._formatDateTime(newDate, 't');
                break;
            default:
                newDateFormatted = newDate.toString();
        }
        if (dateTimeElement.tagName.toUpperCase() == 'SPAN') {
            dateTimeElement.innerText = newDateFormatted;
        }
        else if (dateTimeElement.tagName.toUpperCase() == 'INPUT') {
            dateTimeElement.value = newDateFormatted;
        }
    }

}
, get_DateTimeFormatString: function() {
    return this._DateTimeFormatString;
}
, set_DateTimeFormatString: function(value) {
    this._DateTimeFormatString = value;
}
    //formats and returns date object
, _formatDateTime: function(newDate, format) {
    var month = this._prefixZeros(newDate.getMonth());
    var day = newDate.getDate();

    switch (format.toLowerCase()) {
        case 'd':
            return this._prefixZeros((newDate.getMonth() + 1)) + '/' + this._prefixZeros(newDate.getDate()) + '/' + newDate.getFullYear();
        case 't':
            if (newDate.getHours() > 12) {
                return this._prefixZeros((newDate.getHours() - 12)) + ':' + this._prefixZeros(newDate.getMinutes()) + ' PM';
            }
            else if (newDate.getHours() == 12) {
                return newDate.getHours() + ':' + this._prefixZeros(newDate.getMinutes()) + ' PM';
            }
            else if (newDate.getHours() == 0) {
            return '00:' + this._prefixZeros(newDate.getMinutes()) + ' AM';
            }
            else {
                return this._prefixZeros(newDate.getHours()) + ':' + this._prefixZeros(newDate.getMinutes()) + ' AM';
            }
        default:
            return newDate.toString();
    }
}
, _prefixZeros: function(value) {
    if (value.toString().length == 1)
        return '0' + value;
    else
        return value;
}
}

Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.descriptor = {
    properties: [{ name: 'DateTimeFormatString', type: String }]
}

Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior.registerClass('Microsoft.InformationSecurity.CISF.Web.AjaxExtenders.UTCToLocalExtenderBehavior', Sys.UI.Behavior);
